/**
 * 用户登录js
 */
var login= {	
	//事件绑定
	bindEvent:function(){
		
		$('#btn-login').click(function() {
			$('#btn-login').attr('disabled','disabled');
			var name=$.trim($('#name').val());
			if(name==''){
				$('#alert-warning').css('display','block');
				$('#alertMsg').html('用户名不能为空！');
				return false;
			}
			var pwd=$.trim($('#pwd').val());
			if(pwd==''){
				$('#alert-warning').css('display','block');
				$('#alertMsg').html('密码不能为空！');
				return false;
			}
			login.loginImpl();
		});
		
		//按回车键登录事件
		document.onkeydown = function(e){ 
			var ev = document.all ? window.event : e;
			if(ev.keyCode==13) {
				var name=$.trim($('#name').val());
				if(name==''){
					$('#alert-warning').css('display','block');
					$('#alertMsg').html('用户名不能为空！');
					return false;
				}
				var pwd=$.trim($('#pwd').val());
				if(pwd==''){
					$('#alert-warning').css('display','block');
					$('#alertMsg').html('密码不能为空！');
					return false;
				}
				login.loginImpl();
	 		}
		};
	},
	
	loginImpl:function(){
		$('#alert-warning').css('display','block');
		$('#alertMsg').html('正在登陆中...');
		
		var url = 'common/LoginCtrl/login';
		var name=$.trim($('#name').val());
		var pwd=$.md5($.trim($('#pwd').val()));
		var params = {
			'name':name,
			'pwd':pwd
		};

        jQuery.ajax({
            type : 'POST',
            url : APP_ADDRESS+url,
            contentType : 'application/json',
            data : JSON.stringify(params),
            dataType : 'json',
            timeout : 30000, // 30秒超时
            success : function(data, textStatus) {
                $('#btn-login').attr('disabled',false);
                $('#alert-warning').css('display','none');
                if (data.resultCode != 1) {
                    $('#alert-warning').css('display','block');
                    $('#alertMsg').html(data.msg);
                }else{
                    var url=APP_ADDRESS+'mgr/mgrTab.jsp';
                    jsUtils.gotoUrl(url,null);
                }

                if(window.parent.setLiAbled){
                    window.parent.setLiAbled();
                }
            },
            error : function(result) {

                console.log(result);
                //alert(result.status+'请求错误，请稍后再试！');
                if(window.parent.setLiAbled){
                    window.parent.setLiAbled();
                }
            }
        });
	},
    
	init : function() {
		login.bindEvent();
	}
};

$(document).ready(function() {
	login.init();
});

